<template>
  <div/>
</template>

<script>
import store from '@/store'
import {providerLogin} from '@/api/system/login'
import {isEmpty} from '@/utils/validate'
import {Loading} from 'element-ui'

export default {
  created() {
    const currentPage = this;
    const url = '/login?' + currentPage.$route.fullPath.split('?')[1];
    const loadingInstance = Loading.service({fullscreen: true})
    providerLogin(url).then(res => {
      loadingInstance.close()
      if (res.data.cn === null) {
        this.$message.error('登录失败')
        window.location.href = (process.env.VUE_APP_ROUTER_BASE)
        return
      } else {
        sessionStorage.setItem('bdpUserId', res.data.id)
        sessionStorage.setItem('bdpUserCn', res.data.cn)
        sessionStorage.setItem('bdpUserSn', res.data.sn)
        sessionStorage.setItem('bdpUserPermissions', res.data['userPermissions'])
        sessionStorage.setItem('bdpRolePermissions', res.data['rolePermissions'])
        sessionStorage.setItem('bdpGroupPermissions', res.data['groupPermissions'])
        console.log(store.getters.addRouters)
      }
      if (!isEmpty(sessionStorage.getItem('callbackUrl'))) {
        console.log(sessionStorage.getItem('callbackUrl'))
        if (sessionStorage.getItem('callbackUrl').indexOf(process.env.VUE_APP_ROUTER_BASE) === -1) {
          window.location.href = (process.env.VUE_APP_ROUTER_BASE + sessionStorage.getItem('callbackUrl')).replace('//', '/')
        } else {
          window.location.href = (sessionStorage.getItem('callbackUrl'))
        }
      } else {
        window.location.href = (process.env.VUE_APP_ROUTER_BASE)
        console.log(sessionStorage)
      }
      sessionStorage.removeItem('callbackUrl')
    })
  }

}
</script>
